<template>
    <list-layout>
        <div slot="search">
            <el-form :model="queryParams" :inline="true" ref="queryForm" v-show="showSearch" label-width="88px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="申请单号" prop="projectName">
                            <el-input v-model="queryParams.projectName" :placeholder="$t('public.placeholder',{placeholder: $t('basicData.inspectProjectName')})" size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="主题" prop="projectName">
                            <el-input v-model="queryParams.projectCode" :placeholder="$t('public.placeholder',{placeholder: '主题'})" size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="相关产品" prop="projectType">
                            <el-select v-model="queryParams.projectType" multiple filterable size="small" reserve-keyword :placeholder="$t('public.inputPl')+$t('basicData.projectType')">
                                <el-option v-for="item in dict.type.NYIQC_PROJECT_TYPE" :key="item.value" :label="item.label" :value="item.label">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="发起原因" prop="projectName">
                            <el-input v-model="queryParams.projectCode" :placeholder="$t('public.placeholder',{placeholder: '发起原因'})" size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="申请人" prop="projectName">
                            <el-input v-model="queryParams.projectCode" :placeholder="$t('public.placeholder',{placeholder: '申请人'})" size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="状态" prop="projectType">
                            <el-select v-model="queryParams.projectType" multiple filterable size="small" reserve-keyword :placeholder="$t('public.inputPl')+$t('basicData.projectType')">
                                <el-option v-for="item in dict.type.NYIQC_PROJECT_TYPE" :key="item.value" :label="item.label" :value="item.label">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item :label="$t('costPassThrough.applicationTime')" prop="inspectionTime">
                            <el-date-picker style="width: 204px" v-model="queryParams.inspectionTime" size="small" type="daterange" value-format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="6" align="left">
                        <el-form-item class="right-btns">
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.query')}}</el-button>
                            <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

        </div>
        <div class="app-container">
            <div class="container-header">
                <div class="header-left">
                    临时指示
                </div>
            </div>
            <el-row :gutter="10" class="table-action-wrap btnGroup">
                <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['ny-iqc:project:add']">{{$t('public.add')}}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button icon="el-icon-delete" size="mini" class="deleteitemBtn" @click="handleApproval" v-hasPermi="['ny-iqc:project:remove']">{{$t('public.delete')}}</el-button>
                </el-col>
                <!-- <el-col :span="1.5">
                    <el-button class="importitemBtn" size="mini" @click="handleImport" v-hasPermi="['ny-iqc:project:import']"><img src="@/assets/images/icons/daoru.png" alt=""> {{$t('public.import')}}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button class="exportitemBtn" size="mini" @click="handleExport" v-hasPermi="['ny-iqc:project:export']"><img src="@/assets/images/icons/daochu.png" alt=""> {{$t('public.export')}}</el-button>
                </el-col> -->
            </el-row>
            <div class="container-table">
                <el-table :height="tableHeight" border stripe v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">

                    <el-table-column type="selection" width="55" align="center" />
                    <!--          序号-->
                    <el-table-column :label="$t('public.serialNum')" type="index" width="55" align="center" />
                    <!--          项目类型-->
                    <el-table-column label="申请单号" align="left" prop="projectType" minWidth="160" />
                    <!--          检验项目名称-->
                    <el-table-column label="物料编码" align="left" prop="projectName" minWidth="160" />
                    <el-table-column label="主题" align="left" prop="projectCode" minWidth="160" />
                    <!--          描述-->
                    <el-table-column label="相关产品型号/代码" align="left" prop="description" minWidth="160" show-overflow-tooltip />
                    <el-table-column :label="$t('costPassThrough.reasonForInitiating')" align="left" prop="description" minWidth="360" show-overflow-tooltip />
                    <el-table-column :label="$t('inventoryManagement.effectDate')" align="left" prop="projectCode" minWidth="160" />
                    <el-table-column :label="$t('costPassThrough.applicant')" align="left" prop="projectCode" minWidth="110" />
                    <el-table-column :label="$t('costPassThrough.applicationTime')" align="left" prop="projectCode" minWidth="160" />
                    <el-table-column :label="$t('task.tasktype')" align="left" prop="projectCode" minWidth="100" />
                    <el-table-column :label="$t('public.opera')" fixed="right" align="center" width="160">
                        <template slot-scope="scope">
                            <div class="action-wrap">
                                <table-body-btn type="gwedit" @click.native="handleEdit(scope.row)" v-hasPermi="['ny-iqc:project:getInfo']" />
                                <table-body-btn type="gwdelete" @click.native="handleDelete(scope.row)" v-hasPermi="['ny-iqc:project:remove']" />
                            </div>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
            </div>
            <!-- 导入对话框 -->
            <import-dialog :visible.sync="auditProjectVisible" @callback="uploadCallback" :action="'/ny-iqc/project/import'" :templateUrl="'/static/auditProject.xlsx'" />
            <!-- 新增|编辑｜查看 弹框 -->
            <detail :visible.sync="visible" v-model="form" :type="dialogType" @refresh="getList()"></detail>

        </div>
    </list-layout>
</template>

<script>
import {refreshCache,} from '@/api/basic/dict/type'
import {delProject, listProject} from '@/api/basic/project'
import {getToken} from '@/utils/auth'
import Detail from './detial.vue'
import importDialog from '../components/importDialog'
import {mapState} from 'vuex'
//检验管理-检验项目
export default {
    name: 'auditProject',
    dicts: [
        'NYIQC_PROJECT_TYPE', //量具类型
    ],
    components: { Detail, importDialog },
    data() {
        return {
            dialogType: 1, //1:新增  2：编辑 3：查看
            auditProjectVisible: false,
            dialogForm: {},
            deviceOptions: [],
            factoryOptions: [],
            direction: 'rtl',
            addInspection: false,
            visible: false,
            drawer: false,
            approvalOpen: false,
            viewModel: {
                inspectionInformation: '0',
            },
            editModel: {
                inspectionInformation: '0',
            },
            approvalForm: {
                status: '1',
                textarea: '',
            },
            selBtn: 1,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            tableHeight: 'calc(100vh - 308px)',
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                projectName: '',
                projectCode: '',
                projectType: [],
            },
            // 表单参数
            form: undefined,
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: '检验项目导入',
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: 'Bearer ' + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + '/ny-iqc/project/import',
            },
            fullscreen: false,
            names: [],
        }
    },
    created() {
        this.getList()
    },
    computed: {
        ...mapState({
            isFold: (state) => state.app.isFold,
        }),
    },
    watch: {
        isFold(val) {
            if (val === true) {
                this.tableHeight = 'calc(100vh - 308px)'
            } else {
                this.tableHeight = 'calc(100vh - 396px)'
            }
        },
    },
    methods: {
        drawerClose() {
            this.drawer = false
        },
        uploadCallback() {
            this.getList()
        },
        /** 查询字典类型列表 */
        getList() {
            this.loading = true
            let params = JSON.parse(JSON.stringify(this.queryParams))
            if (params.projectType && params.projectType.length) {
                params.projectType = params.projectType.join(',')
            } else {
                params.projectType = ''
            }
            console.log(params)
            listProject(params).then((response) => {
                if (response.code == 200 && response.rows) {
                    this.typeList = response.rows
                    this.total = response.total
                    this.loading = false
                }
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {}
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.form = {}
            this.dialogType = 1
            this.visible = true
        },
        handleApproval() {
            if (this.ids.length === 0) {
                this.$modal.msgWarning(this.$t('formValidation.pleaseSelectAtLeastOneInspectionItem'))
                return
            }
            this.handleDelete()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.names = selection.map((item) => item.projectName)
            // this.single = selection.length!=1
            // this.multiple = !selection.length
        },

        deviceRemoteMethod(e) {
            console.log(e)
            // listAllProjectMessage({deviceName:e}).then(res=>{
            //   this.deviceOptions = res.data
            // })
        },
        handleDeviceChange(e) {
            this.queryParams.deviceName = e.join(',')
        },
        handleProjectTypeChange(e) {
            this.queryParams.gaugeType = e.join(',')
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const dictIds = (row && row.id) || this.ids
            const projectNames = (row && row.projectName) || this.names
            this.$modal
                .confirm(
                    '是否确认删除检验项目名称为"' + projectNames + '"的数据项？'
                )
                .then(function () {
                    return delProject(dictIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('processDefinition.successfullyDeleted'))
                })
                .catch(() => {})
        },
        /** 查看按钮操作 */
        handleView(row) {
            this.dialogType = 3
            this.form = row
            this.visible = true
            // if(row.hasRead == 0){
            //   readNotice(row.noticeId).then(res=>{
            //     if(res.code == 200){
            //       this.handleQuery()
            //     }
            //   })
            // }
        },
        /** 编辑按钮操作 */
        handleEdit(row) {
            this.dialogType = 2
            this.form = row
            this.visible = true
        },
        /** 关闭抽屉操作 */
        handleClose(row) {
            this.drawer = false
        },
        /** 导入按钮操作 */
        handleImport() {
            this.auditProjectVisible = true
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download(
                'system/saas/user/importTemplate',
                {},
                `user_template_${new Date().getTime()}.xlsx`
            )
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false
            this.upload.isUploading = false
            this.$refs.upload.clearFiles()
            this.$alert(response.msg, '导入结果', {
                dangerouslyUseHTMLString: true,
            })
            this.getList()
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit()
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                '/ny-iqc/project/export',
                {
                    ...this.queryParams,
                },
                `project_${new Date().getTime()}.xlsx`
            )
        },
        /** 刷新缓存按钮操作 */
        handleRefreshCache() {
            refreshCache().then(() => {
                this.$modal.msgSuccess('刷新成功')
            })
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-card {
    padding-top: 20px;
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding-bottom: 20px;
}
::v-deep .el-aside {
    padding: 0;
    margin-bottom: 0;
}
.content-style {
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 2px;
    background-color: white;
    margin-top: 10px;
}
::v-deep .el-button-group .selBtn {
    background: #fff;
    border: 1px solid #0b7be3;
    color: #0b7be3;
}
::v-deep .el-button-group .unselBtn {
    background: #fff;
    border: 1px solid #ededf2;
    color: #666666;
}
.header-left {
    font-size: 14px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    padding: 3px 0 10px;
    display: inline-flex;
    justify-content: center;
    border-bottom: 2px solid #0b7be3;
}
.header-right {
    float: right;
}
.container-header {
    border-bottom: 1px solid #ededf2;
}
.container-table {
    float: left;
    //margin-top:6px;
    width: 100%;
}
.mb8 {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.drawer-footer {
    display: flex;
    position: relative;
    z-index: 9999;
    bottom: -40px;
    height: 40px;
    justify-content: center;
    width: 100%;
}
.drawer-close {
    background: #f7f8fa;
    border-color: #f7f8fa;
}

.right-btns ::v-deep .el-button {
    margin-top: 0;
}
.app-container {
    padding: 0;
}
.table-action-wrap {
    padding: 12px 0;
}
</style>
